package com.creative.accountsdk.teacher;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.R;
import com.creative.accountsdk.ThirdLoginActivity;
import com.creative.accountsdk.adapter.ClassAdapter;
import com.creative.accountsdk.bean.ClassInfo;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.ClassListProtocol;

import org.json.JSONArray;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
import com.creative.accountsdk.adapter.ClassAdapter.Callback;
import com.creative.accountsdk.net.account.TeachAddClassProtocol;
import android.widget.AdapterView.OnItemClickListener;
import java.util.List;
/**
 * 教师加入班级
 *    班级列表
 *
 * @author  David
 * @version 1.0
 * @created 2015-11-25
 */
public class ClassListActivity extends ThirdLoginActivity implements OnItemClickListener,Callback {

    private  Dialog    progress;
    private  Context   context;
    private  ListView  lvClass;

    private  ClassAdapter  myClassAdapter;

    private List<ClassInfo> classList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.teacher_class_list);

        this.initData();
        this.initview();
        this.setListener();
    }

    private void initData(){
        DataCenter.init(this);
        context = this;

        progress = DialogUtils.createProgress(context);
        progress.show();

        final ClassListProtocol protocol = new ClassListProtocol();

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(context, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, context)) {
                    if (null != protocol.classList) {
                        classList = protocol.classList;
                        myClassAdapter = new ClassAdapter(context,ClassListActivity.this);
                        myClassAdapter.setData(protocol.classList);
                        lvClass.setAdapter(myClassAdapter);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                progress.cancel();

            }
        });
    }

    private void  initview(){
        lvClass = (ListView)findViewById(R.id.lv_class_list);
        lvClass.setDivider(null);
    }

    private void setListener(){

    }

    @Override
    public void click(View v) {
     /*   Toast.makeText(context, "listview的内部的按钮被点击了！，位置是-->" + (Integer) v.getTag() + "," +
                "内容是-->" + otherClassList.get((Integer) v.getTag()).name, Toast.LENGTH_SHORT).show();*/
        long classid = classList.get((Integer) v.getTag()).id;
        if(v.getId()==R.id.btn_add_class){
            Log.e("","发送网络请求 ");

            sendAddClass(classid);
        }
        else if(v.getId()==R.id.iv_add_class){
            toSlectCourse(classid);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    /**
     * 加入班级网络请求
     */
    private void sendAddClass(final long  classid){
        progress = DialogUtils.createProgress(context);
        progress.show();

        final TeachAddClassProtocol protocol = new TeachAddClassProtocol(classid);

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(context, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, context)) {
                    Log.e("Tag", "返回加入班级信息.." + protocol.returnMap);
                    toSlectCourse(classid);
                }

            }
        });
    }

    private void  toSlectCourse(long classid){
        Intent  intent =new Intent(context,SelectCourseActivity.class);
          intent.putExtra("classid",classid);
        this.startActivity(intent);
    }
}
